package taxi

class CrudController {

	def beforeInterceptor = {}
	
	 def sessionFactory
	
	 def index() {
		 flash.clear();
		 def model = list()
		 model
	 }
 
	 def list = {
		 flash.clear()
		 def criteria = entity.createCriteria()
		 params.max = Math.min(params.max ? params.int('max') : 10, 100)
		 def entities = criteria.list(query, max: params.max, offset: params.offset)
		 def filters = params
		
 
		 def model = [entityInstanceList: entities, entityInstanceTotal: entities.totalCount, filters: filters]
		
		 if(entities.totalCount<1){
			 flash.warning = message(code: 'default.notresult.message')
		 }
 
		 if (request.xhr && !params.dontSearch) {
			 render(template: "grid", layout:"ajax", model: model)
		 }else if(params.dontSearch.equals("true")){
			 render(template: "form", layout:"ajax", model: model)
		 }else {
			 model
		 }
	 }
	
	 def novo() {
		 print 'oi'
		 flash.clear();
		 render(template: "form", layout:"ajax" , model: [entityInstance: entity.newInstance(params)])
	 }
	
	 def beforeSave(entityInstance,model){}
	
	 def setCollettions(entityInstance){}
	
	 def getInstanceEntity(){
		 def entityInstance
		 if(params.id){
			 entityInstance = entity.get(params.id)
			 entityInstance.properties = params
			 setCollettions(entityInstance)
		 } else {
			 entityInstance = entity.newInstance(params)
			 setCollettions(entityInstance)
		 }
		 entityInstance
	 }
	
	 def save() {
		 flash.clear();
		 def model = [:]
		 def entityInstance
		 boolean edit = params.id ? true:false;
		 boolean editPai = params.editPai ? true:false;
		 entityInstance = getInstanceEntity()
		 beforeSave(entityInstance,model);
		 if(entityInstance.errors.getErrorCount() < 1 && entityInstance.validate()) {
			 print '1'
			 if (entityInstance.save(flush: true, failOnError:true)) {
				 print '2'
				 	 afterSave(entityInstance,model);
				 if(edit){
					 flash.message = message(code: 'default.updated.message')
				 }else{
					 flash.message = message(code: 'default.created.message')
				 }
			 }else{
				 if(edit){
					 flash.error = message(code: 'default.dont.updated.message')
				 }else{
					 flash.error = message(code: 'default.dont.created.message')
				 }
			 }
		 }
		 print '3'
		 if(!((edit || editPai) || !entityInstance.validate())){
			 entityInstance = entity.newInstance()
		 }
		 model.put('entityInstance', entityInstance)
		 if(params.dontSearch){
			 model.put('entityInstanceList', entity.list())
		 }
		
		 render(template: "form", layout:"ajax", model: model)
	 }
	
	 def afterSave(entityInstance,model){}
	
	 def edit() {
		 flash.clear();
		 def entityInstance = entity.get(params.id)
		 render(template: "form", layout:"ajax", model: [entityInstance: entityInstance])
	 }
 
	 def delete() {
		 flash.clear();
		 def entityInstance = entity.get(params.id)
		 if (entityInstance) {
			 try {
				 entity.withTransaction {
					 entityInstance.delete(flush: true)
					 flash.message = message(code: 'default.deleted.message', args: [message(code: '${entity.getSimpleName()}.label', default: entity.getSimpleName()), params.id])
				 }
			 }
			 catch (org.springframework.dao.DataIntegrityViolationException e) {
				 flash.error = message(code: 'default.not.deleted.message', args: [message(code: '${entity.getSimpleName()}.label', default: entity.getSimpleName()), entityInstance.toString()])
			 }
		 }
		 else {
			 flash.message = message(code: 'default.not.found.message', args: [message(code: '${entity.getSimpleName()}.label', default: entity.getSimpleName()), params.id])
		 }
		 list()
	 }
	
}
